package com.web.user;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.biz.AddressBizImpl;
import com.biz.IAddressBiz;
import com.entity.Address;
import com.entity.UserInfo;
import com.util.PageBean;

public class AddressServlet extends HttpServlet {

	IAddressBiz adbiz = new AddressBizImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		UserInfo us = (UserInfo) session.getAttribute("loger");
		if (us != null) {
			String act = request.getParameter("act");
			if (act == null) {
				//分页展示地址
				showByPage(request, response, out, session, us);
			} else if (act.equals("add")) {
				//增加
				add(request, out, us);
			} else if (act.equals("del")) {
				//删除
				del(request, out);
			} else if (act.equals("get")) {
				//取地址详细信息
				get(request, response, session);
			} else if (act.equals("mod")) {
				//修改地址信息
				mod(request, out);
			} else if (act.equals("modismain")) {
				//设置地址为默认地址
				modisadmin(request, out, us);
			}
		} else {
			out.print("<script>location='../login.jsp';</script>");
		}
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * 分页展示地址
	 * 
	 * @throws IOException
	 * @throws ServletException
	 */
	public void showByPage(HttpServletRequest request, HttpServletResponse response, PrintWriter out,
			HttpSession session, UserInfo us) throws ServletException, IOException {

		/******** 分页显示 **********/
		System.out.println("******分页显示地址******");
		String uid = "" + us.getId();
		String mypage = request.getParameter("page");
		if (mypage == null)
			mypage = "1";
		int currPage = Integer.parseInt(mypage);
		if (currPage < 1)
			currPage = 1;
		PageBean<Address> adpb = adbiz.showAdderssByPage(currPage, 10, uid);
		session.setAttribute("adpb", adpb);
		
		String isorder = request.getParameter("orders");
		
		System.out.println("---------------------------------");
		System.out.println("orders="+isorder);
		System.out.println("---------------------------------");
		
		if (isorder != null && isorder.equals("1")) {
			request.setAttribute("orders", 1);
			String pid = request.getParameter("pid");
			String num = request.getParameter("num");
			String comments = request.getParameter("comments");
			System.out.println("pid=" + pid);
			session.setAttribute("pid", pid);
			session.setAttribute("num", num);
			session.setAttribute("comments", comments);
			request.getRequestDispatcher("showAddress.jsp").forward(request, response);
		} else if (isorder != null && isorder.equals("2")) {
			request.setAttribute("orders", 2);
			String[] scids=request.getParameterValues("check");
			session.setAttribute("scids", scids);
			request.getRequestDispatcher("showAddress.jsp").forward(request, response);
		} else {
			System.out.println("非提交订单");
			response.sendRedirect("showAddress.jsp");
		}
	}

	/**
	 * 增加地址
	 */
	public void add(HttpServletRequest request, PrintWriter out, UserInfo us) {
		/******** 增加 **********/
		System.out.println("******增加地址******");
		String uid = "" + us.getId();
		String contacts = request.getParameter("contacts");
		String phone = request.getParameter("phone");
		String country = request.getParameter("country");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String address = request.getParameter("address");

		Address ad = new Address();
		ad.setUid(uid);
		ad.setContacts(contacts);
		ad.setPhone(phone);
		ad.setCountry(country);
		ad.setProvince(province);
		ad.setCity(city);
		ad.setArea(area);
		ad.setAddress(address);

		if (new AddressBizImpl().addAddress(ad) <= 0) {
			out.print("<script>alert('添加失败');history.back();</script>");

		} else {
			out.print("<script>location='address';</script>");
		}
	}

	/**
	 * 删除地址
	 */
	public void del(HttpServletRequest request, PrintWriter out) {
		/******** 删除 **********/
		System.out.println("******删除地址******");
		String id = request.getParameter("id");
		if (adbiz.delAddress(id) <= 0) {
			out.print("<script>alert('删除失败');</script>");
		}
		out.print("<script>location='address';</script>");
	}

	/**
	 * 获取地址信息
	 * 
	 * @throws IOException
	 */
	public void get(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
		/******** 获取单个地址信息 **********/
		System.out.println("******获取地址信息******");
		String id = request.getParameter("id");
		Address myad = adbiz.getAddress(id);
		session.setAttribute("ad", myad);
		response.sendRedirect("modAddress.jsp");
	}

	public void mod(HttpServletRequest request, PrintWriter out) {
		/******** 修改地址信息 **********/
		String id = request.getParameter("id");
		String contacts = request.getParameter("contacts");
		String phone = request.getParameter("phone");
		String country = request.getParameter("country");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String address = request.getParameter("address");

		Address ad = new Address();
		ad.setId(id);
		ad.setContacts(contacts);
		ad.setPhone(phone);
		ad.setCountry(country);
		ad.setProvince(province);
		ad.setCity(city);
		ad.setArea(area);
		ad.setAddress(address);
		if (new AddressBizImpl().modAddress(ad) <= 0) {
			out.print("<script>alert('修改失败');history.back();</script>");
		} else {
			out.print("<script>location='address';</script>");
		}
	}

	public void modisadmin(HttpServletRequest request, PrintWriter out, UserInfo us) {
		String id = request.getParameter("id");
		String uid = "" + us.getId();
		if (adbiz.modAddressIsmain(id, uid) <= 0) {
			out.print("<script>alert('设置失败');</script>");
		}
		out.print("<script>location='address';</script>");
	}
}
